package com.lottoad.android.activity.notify.adapter;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.lottoad.android.R;
import com.lottoad.android.cronos.data.CNLottoAdPartnerData;
import com.lottoad.android.cronos.datahandler.CNBaseDataSet;
import com.lottoad.android.util.LottoAdImageDownloaderFile;
import com.lottoad.android.util.RecycleUtils;
//import com.lottoad.android.util.LottoAdImageDownloader;

public class LottoAdPartnerAdapter extends BaseAdapter{

	protected LayoutInflater mInflater = null;
	protected ArrayList<CNBaseDataSet> mDataSetList = null;
	//private LottoAdImageDownloader mImageDownloader = null;
	private LottoAdImageDownloaderFile mImageDownloader = null;
	
	private List<WeakReference<View>> mRecycleList = new ArrayList<WeakReference<View>>();
	
	public void recycle() {
		for (WeakReference<View> ref : mRecycleList) {
			RecycleUtils.recursiveRecycle(ref.get());
		}
	}
	
	private class ViewHolder {
		ImageView mPartnerImg = null;
		ProgressBar mProgressBar;
	}
	
	public LottoAdPartnerAdapter(Context context){
		this.mInflater = LayoutInflater.from(context);
		//this.mImageDownloader = new LottoAdImageDownloader(context);
		this.mImageDownloader = new LottoAdImageDownloaderFile(context);
	}
	
	public void setDataSetList(ArrayList<CNBaseDataSet> dataSetList) {
		this.mDataSetList = dataSetList;
	}
	
	public void clear(){
		if(this.mDataSetList != null){
			this.mDataSetList.clear();
			this.notifyDataSetChanged();
		}
	}
	
	@Override
	public int getCount() {
		if (this.mDataSetList != null) {
			return this.mDataSetList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (this.mDataSetList != null) {
			return this.mDataSetList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null){
			convertView = this.mInflater.inflate(R.layout.lottoad_menu_notify_partner_row, null);
			holder = new ViewHolder();
			
			holder.mPartnerImg = (ImageView) convertView.findViewById(R.id.imageView_partner);
			holder.mProgressBar = (ProgressBar) convertView.findViewById(R.id.ProgressBarLayout);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		CNLottoAdPartnerData dataSet = (CNLottoAdPartnerData) getItem(position);
		
		if(dataSet != null){
			if(holder.mPartnerImg.getDrawable() != null){
				holder.mPartnerImg.getDrawable().setCallback(null);
			}
			//mImageDownloader.download(dataSet.partnerImg, holder.mPartnerImg, position, holder.mProgressBar);
			mImageDownloader.download(dataSet.partnerImg, holder.mPartnerImg, holder.mProgressBar);
		}
		
		return convertView;
	}
}
